package com.limao.CacheManager;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.CrowdUserAuthClass;
import com.limao.DBDataClass.userRmbDataClass;
import com.limao.DBProvider.UserRmbProvider;
import com.limao.Model.PublicData;
import com.limao.cache.BasicModule;

public class user_rmbCacheManager extends BasicModule{

	private UserRmbProvider provider;
	
	private static user_rmbCacheManager instance;

    public static user_rmbCacheManager getInstance() {
        return instance;
    }

    private Cache<String, userRmbDataClass> user_rmbManagerCache;


    public user_rmbCacheManager() {
      	super("userRmbCacheManager");
      	user_rmbManagerCache = CacheFactory.createCache("cd_userRmbDataCache");
        initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    public void ClearObjectCache(String userid)
    {
    	userRmbDataClass user =user_rmbManagerCache.get(userid);
    	if(user!=null)
    	{
    		user_rmbManagerCache.remove(user.userid);
    	}
    }  

	//查询用户id
	public userRmbDataClass load_User_Rmb(int id){
		String key = id + "";
		userRmbDataClass obj = user_rmbManagerCache.get(key);
		if (obj == null) {
			obj = provider.load_User_Rmb(id);
			if (obj != null) {
				user_rmbManagerCache.put(key, obj);
			}
		}
		
		return obj;
	} 
	
	
	//团队人数加1
	public userRmbDataClass update_User_Mannum(int id){
		String key = id + "";
		userRmbDataClass	obj = provider.update_User_Mannum(id);
		if (obj != null) {
				user_rmbManagerCache.put(key, obj);
		}
		return obj;
	} 
	
	
	
	//&新鲜宝     取用户收益统计列表
	public PublicData get_Sys_RMB_List(int id,int curpage)
	{
		return provider.get_Sys_RMB_List(id,curpage);
	}



	//&新鲜宝     取用户现货瓶数记录列表
	public PublicData get_Sys_RMB_List4(int id,int curpage)
	{
		return provider.get_Sys_RMB_List4(id,curpage);
	}
	
	//新鲜宝    取账单列表
	public PublicData get_Sys_RMB_List3(int id,int curpage)
	{
		return provider.get_Sys_RMB_List3(id,curpage);
	}
	
	
	//&新鲜宝 取用户团队列表
	public PublicData get_Sys_MANS_List(int id,String keyword,int curpage)
	{
		return provider.get_Sys_MANS_List(id,keyword,curpage);
	}	
	
	

	//&新鲜宝  排行榜
	public PublicData getRankInfo(int type,int curpage)
	{
		return provider.getRankInfo(type,curpage);
	}	
	
	
	//&新鲜宝  购买分成
	public CrowdUserAuthClass BuyCut(float totalRmb,int goodsnum,int userid,int userViplevel,String orderid,String projectid,String projectname)
	{
		return provider.BuyCut(totalRmb,goodsnum,userid,userViplevel,orderid,projectid,projectname);
	}
	
	//&新鲜宝  注册活跃值增加
	public CrowdUserAuthClass InvitePreCut(int userid,int type,float totalrmb,int orderid)
	{
		CrowdUserAuthClass tmp=null;
		if(type==2) //2登陆签到
		{
			boolean lockSucess=false;
			Lock lock = CacheFactory.getLock(userid+"", CrowdUserAuthCache.getInstance().getCache()); //加锁
	        try {
	        	try {
					lockSucess=lock.tryLock(8, TimeUnit.SECONDS); //最长锁8秒
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
		        if(lockSucess)
		        {
		        	CrowdUserAuthCache.getInstance().getCache().get(userid+"");  //检验锁
		        	tmp=provider.InvitePreCut(userid,type,totalrmb,orderid);
		        }
		    } finally {
			   	if(lockSucess)
			    	lock.unlock();//解锁
		    }
		}
		else
		{
			tmp=provider.InvitePreCut(userid,type,totalrmb,orderid);
		}
        return tmp;
	}
	
	//更新用户的非冻结数值
	public userRmbDataClass Update_User_Rmb(int id){
		String key = id + "";
		userRmbDataClass obj = user_rmbManagerCache.get(key);
		if (obj == null) {
			obj = provider.load_User_Rmb(id);
			if (obj != null) {
				user_rmbManagerCache.put(key, obj);
			}
		}
		
		return obj;
	} 
	

		
	
//*********************************************************************************
	
	private void initProvider() {
		if (provider == null) {
			try {
				provider = UserRmbProvider.getInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}


}
